package com.sky.user.controller.admin;

import com.alibaba.nacos.shaded.io.grpc.Internal;
import com.sky.common.config.JwtProperties;
import com.sky.common.constant.JwtClaimsConstant;
import com.sky.common.constant.StatusConstant;
import com.sky.common.context.BaseContext;
import com.sky.common.result.PageResult;
import com.sky.pojo.dto.EmployeeDTO;
import com.sky.pojo.dto.EmployeeLoginDTO;
import com.sky.pojo.dto.EmployeePageQueryDTO;
import com.sky.pojo.entity.Employee;
import com.sky.common.result.Result;
import com.sky.user.service.EmployeeService;
import com.sky.pojo.vo.EmployeeLoginVO;
import com.sky.common.utils.JwtUtil;
import io.swagger.annotations.ApiOperation;
import jdk.net.SocketFlow;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.HashMap;
import java.util.Map;

/**
 * 员工管理
 */
@RestController
@RequestMapping("/admin/employee")
@Slf4j
@RequiredArgsConstructor
public class EmployeeController {

    private final EmployeeService employeeService;

    private final JwtProperties jwtProperties;

    /**
     * 登录
     *
     * @param employeeLoginDTO
     * @return
     */
    @PostMapping("/login")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录：{}", employeeLoginDTO);

        Employee employee = employeeService.login(employeeLoginDTO);
        if(employee.getStatus().equals(StatusConstant.DISABLE)){
            return Result.error("该账户已被冻结");
        }

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();

        return Result.success(employeeLoginVO);
    }

    /**
     * 退出
     *
     * @return
     */
    @PostMapping("/logout")
    public Result<String> logout() {
        return Result.success();
    }

    // 测试使用的
    @GetMapping("/hello")
    public Result<String> hello(){
        return Result.success("当前用户Id为：" + BaseContext.getCurrentId());
    }

    // 添加员工
    @PostMapping
    public Result save(@RequestBody EmployeeDTO employeeDTO){
        log.info("新添加员工：{}",employeeDTO);
        employeeService.save(employeeDTO);
        return Result.success();
    }

    // 分页查询员工
    @GetMapping("/page")
    public Result<PageResult> page(EmployeePageQueryDTO employeePageQueryDTO){
        log.info("分页查询员工，参数为：{}",employeePageQueryDTO);
        PageResult pageResult =  employeeService.page(employeePageQueryDTO);
        return Result.success(pageResult);
    }

    // 修改账号状态
    @PostMapping("/status/{status}")
    public Result startOrStop(@PathVariable("status") Integer status,Long id){
        log.info("修改员工账号状态：{}，{}",status,id);
        employeeService.startOrStop(status,id);
        return Result.success();
    }

    // 获取单个员工信息
    @GetMapping("/{id}")
    @ApiOperation("根据Id获取信息")
    public Result<Employee> getById(@PathVariable Integer id){
        log.info("获取员工信息id为：{}",id);
        Employee employee = employeeService.getById(id);
        return Result.success(employee);
    }

    // 修改员工功能
    @PutMapping
    @ApiOperation("根据Id来修改员工")
    public Result<Employee> update(@RequestBody EmployeeDTO employeeDTO){
        log.info("修改员工信息：{}",employeeDTO);
        employeeService.update(employeeDTO);
        return Result.success();
    }
}
